﻿using FunctionalCLR.Core;
using Zove.Runtime.FunctionalCLR;

namespace Zove.Runtime {
    public class ZoveParserFactory : IParserFactory {

        public IFunctionManager FunctionManager {
            get;
            set;
        }

        public Parser CreateParser(string expr) {
            return CreateParser(expr, null);
        }

        public Parser CreateParser(string expr, object context) {
            RuntimeModel runtimeModel = context as RuntimeModel;

            ZoomParser parser = new ZoomParser(expr, runtimeModel);
            parser.ConstantManager
                = DefaultDependencyManager.Instance.ConstantManager;
            parser.FunctionManager 
                = ZoveDependencyManager.Instance.FunctionManager;
            parser.ParserFactory = this;

            return parser;
        }

        public object Evaluate(string expr) {
            return this.Evaluate(expr, null);
        }

        public object Evaluate(string expr, object context) {
            RuntimeModel runtimeModel = context as RuntimeModel;

            Parser pa = CreateParser(expr.Trim(), runtimeModel);
            Node root = pa.Parse();
            string value = root.Evaluate().ToString();

            return value;
        }
    }
}
